<?php
namespace Admin\Controller;
use Think\Controller;

class TrainingController extends BaseController
{
    /**
     * 列表
     */
    public function index()
    {
        $title = '培训计划管理';

        parent::index($title);
    }

    /**
     * 培训课程设置
     */
    public function courseRelation()
    {
        $id = I('get.id', 0);

        $title = '培训课程设置';
        $this->assign('title', $title);
        $this->assign('id', $id);

        //layout('noHeadTablelayout');
        layout('tablelayout');
        $this->display();
    }


    /*
     * 获取列表数据
     */
    public function GetData()
    {
        $trainings = D('Training');
        $data = $trainings->pageList();

        parent::GetData($data);
    }

    /*
    * 获取课程关联列表
    */
    public function GetRelationData()
    {
        $id = I('get.id', 0);
        $draw = I('draw');

        $Courses = D('Course');
        $trainingCourses = D("TrainingCourse");
        $OriginalData = $Courses->pageList(1);
        $SelectedData = $trainingCourses->relationList($id);
        $data = $OriginalData['data'];

        //格式化数据
        foreach ($data as $k => $v) {
            $data[$k]['IsSelected'] = 0;
            $data[$k]['IsRequired'] = 0;

            foreach ($SelectedData as $ks => $vs) {
                if ($vs['scormid'] == $v['id']) {
                    $data[$k]['IsSelected'] = 1;
                    $data[$k]['IsRequired'] = is_null($vs['isrequired']) ? 0 : $vs['isrequired'];
                }
            }
        }

        //条件过滤后记录数 必要
        $recordsFiltered = $OriginalData['recordsFiltered'];
        //表的总记录数 必要
        $recordsTotal = $OriginalData['recordsTotal'];

        echo json_encode(array(
            "draw" => intval($draw),
            "recordsTotal" => intval($recordsTotal),
            "recordsFiltered" => intval($recordsFiltered),
            "data" => $data
        ), JSON_UNESCAPED_UNICODE);
    }

    /*
     * 保存课程关联
     */
    public function SaveRelation()
    {
        $json = I('post.data');
        $TrainingID = I('post.TrainingID');

        foreach ($json as $k => $v) {
            $ScormID = $v['ScormID'];
            $IsSelected = $v['IsSelected'];
            $IsRequired = $v['IsRequired'];
            $Before = $v['Before'];

            $trainingCourses = D("TrainingCourse"); // 实例化User对象

            $where = " SCORMID=" . $ScormID . " AND TrainingID=" . $TrainingID;
            $data = $trainingCourses->where($where)->find();

            if ($data == null && $Before == 1) {
                continue;
            }

            //before=0,IsSelected=1,insert id
            if ($Before == 0 && $IsSelected == 1 && $data == null) {
                $add['TrainingID'] = $TrainingID;
                $add['SCORMID'] = $ScormID;
                $add['IsRequired'] = $IsRequired;
                $add['SystemID'] = "0";

                $trainingCourses->add($add);
            }
            //before=1,IsSelected=0,delete id
            if ($Before == 1 && $IsSelected == 0) {
                $trainingCourses->where($where)->delete();
            }
            //before=1,IsSelected=1,update id
            if ($Before == 1 && $IsSelected == 1) {
                M('trainingcourse')->where($where)->setField('IsRequired', $IsRequired);
            }
        }

        echo 1;
    }

    /**
     * 新增
     */
    public function Add()
    {
        $id = I('get.id', 0);
        if (!IS_POST) {
            if ($id == 0) {
                $title = '新增培训计划信息';
            } else {
                $title = '修改培训计划信息';
                $data = D('Training')->find($id);
                $this->assign('data', $data);
            }

            $this->assign('title', $title);
            $this->assign('id', $id);
            layout('tablelayout');
            $this->display();
        } else {
            //保存页面
            header("Content-type: text/html;charset=utf-8");
            $data = D('Training');

            if ($data->create()) {
                if ($data->ID == 0) {
                    $result = $data->add();
                } else {
                    $result = $data->save();
                }

                if ($result) {
                    $this->success('操作成功！', U('index', '', ''), 3);
                } else {
                    $this->error('写入错误！');
                }
            } else {
                $this->error($data->getError());
            }
        }
    }

    /**
     * 删除
     */
    public function Delete()
    {
        $id = I('get.id', 0);
        header("Content-type: text/html;charset=utf-8");
        $data = D('Training');

        $result = $data->where('ID=' . $id)->delete();
        if ($result) {
            $this->success('操作成功！', U('index', '', ''), 3);
        } else {
            $this->error('写入错误！');
        }
    }
}